<?php

namespace app\index\service;

use app\common\model\Trade;
use app\index\dao\TradeDao;
use app\index\traits\TradeShareEstimateTrait;
use think\exception\ValidateException;
use think\facade\Config;

/**
 * AXB通信订单
 * Class TradeAxbCommunicationService
 * @package app\index\service
 */
class TradeAxbCommunicationService implements TradableInterface
{
    use TradeShareEstimateTrait;

    // dao
    private TradeDao $tradeDao;

    public function __construct(TradeDao $tradeDao)
    {
        $this->tradeDao = $tradeDao;
    }

    /**
     * 通话后，追加通信币订单
     * @param $memberId
     * @param $sourceId
     * @param $talkTime
     * @param $axbRecord
     * @param $source
     * @return Trade
     */
    public function append($memberId, $sourceId, $talkTime, $axbRecord, $source): Trade
    {
        // 补齐参数，创建Trade
        $tradeType = TRADE_AXB;
        $tradeConfig = Config::get("trade.trades.$tradeType", []);
        $tradeTypeClass = $tradeConfig['className'] ?? '';


        if (is_blank($sourceId)) {
            throw new ValidateException('请选择一个案源');
        }

        $describe = $tradeConfig['name'];

        if (!is_numeric($talkTime) || $talkTime < 0) {
            throw new ValidateException('通话时长应该是正确的数字');
        }

        $num = ceil($talkTime / 60);

        if ($num != 0) {
            $price = $num * Config::get('axb.price', 3);
        } else {
            $price = 1 * Config::get('axb.price', 3);
        }

        // 暂时没说优惠卷
        $showMoney = $price;
        $totalMoney = $price;
        $payMoney = $totalMoney;

        if ($payMoney < 0.01) {
            throw new ValidateException('订单价格应该大于0');
        }

        $tradeParams = [
            'payMethod' => PAYMENT_COIN,
            'memberId' => $memberId,
            'orderNoPre' => 'AXB',
            'describe' => $describe,
            'tradeType' => $tradeType,
            'tradeTypeClass' => $tradeTypeClass,
            'sourceId' => $sourceId,
            'showMoney' => $showMoney,
            'totalMoney' => $totalMoney,
            'payMoney' => $payMoney,
            'num' => $num,
            'payStatus' => Trade::UNPAID,
            'payExpTime' => time() + 3600,
            'settleStatus' => Trade::NOTSETTLE,
            'attach' => [
                'attach' => '',
                'record' => $axbRecord,
                'source' => $source,
            ],
        ];

        $record = $this->tradeDao->insertTrade($tradeParams);

        // 触发订单创建事件
        event('TradeCreated', $record);

        return $record;
    }

    // 完成订单
    public function completionTrade(Trade $trade)
    {
        // 完成购买 

        // 触发订单完成事件
        event('TradeEnded', $trade);

    }

    // 完成退款，收回服务
    public function completionRefundTrade(Trade $trade)
    {

        return;

    }

    // 已取消支付&订单
    public function canceledTrade(Trade $trade)
    {
        return;
    }

    private function getMessageSendService(): MessageSendService
    {
        return app(MessageSendService::class);
    }

}
